package models

import (
	orm "go-papers/database"
	_ "time"
)

type OrganMsg struct {
	Id           int    `json:"id" gorm:"type:int(11) unsigned;primary_key"` //
	OrganId      int    `json:"organId" gorm:"type:int(11);"`                //
	OpenId       string `json:"openId" gorm:"type:varchar(128);"`            //
	OpenUserName string `json:"openUserName" gorm:"type:varchar(255);"`      //
	OpenUserId   int    `json:"openUserId" gorm:"type:int(11);"`             //
	MsgRole      string `json:"msgRole" gorm:"type:varchar(255);"`           //
	Status       int    `json:"status" gorm:"type:smallint(1);"`             //

	DataScope string `json:"dataScope" gorm:"-"`
	Params    string `json:"params"  gorm:"-"`
	BaseModel
}

func (OrganMsg) TableName() string {
	return "organ_msg"
}

// 创建OrganMsg
func (e *OrganMsg) Create() (OrganMsg, error) {
	var doc OrganMsg
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

func (e *OrganMsg) GetByOrganRole(id int, role string) ([]OrganMsg, error) {
	var doc []OrganMsg

	table := orm.Eloquent.Select("*").Table(e.TableName())
	table = table.Where("status = ? ", 1)
	table = table.Where("msg_role = ? ", 1)
	table = table.Where("organ_id = ? ", id)

	if err := table.Find(&doc).Error; err != nil {
		return nil, err
	}
	return doc, nil
}

// 获取OrganMsg
func (e *OrganMsg) Get() (OrganMsg, error) {
	var doc OrganMsg
	table := orm.Eloquent.Table(e.TableName())

	if e.OrganId != 0 {
		table = table.Where("organ_id = ? ", e.OrganId)
	}

	if e.OpenId != "" {
		table = table.Where("open_id = ? ", e.OpenId)
	}

	if e.Status != 0 {
		table = table.Where("status = ? ", e.Status)
	}

	if e.OpenUserName != "" {
		table = table.Where("open_user_name like ? ", "%"+e.OpenUserName+"%")
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

func (e *OrganMsg) GetPageList(organId int) ([]OrganMsg, int, error) {
	var doc []OrganMsg

	table := orm.Eloquent.Select("*").Table(e.TableName())
	table = table.Where("status = ? ", 1)
	table = table.Where("organ_id = ? ", organId)

	if e.OpenId != "" {
		table = table.Where("open_id = ? ", e.OpenId)
	}

	if e.OpenUserName != "" {
		table = table.Where("open_user_name like ? ", "%"+e.OpenUserName+"%")
	}

	var count int

	if err := table.Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 获取OrganMsg带分页
func (e *OrganMsg) GetPage(pageSize int, pageIndex int) ([]OrganMsg, int, error) {
	var doc []OrganMsg

	table := orm.Eloquent.Select("*").Table(e.TableName())

	if e.OrganId != 0 {
		table = table.Where("organ_id = ? ", e.OrganId)
	}

	if e.OpenId != "" {
		table = table.Where("open_id = ? ", e.OpenId)
	}

	if e.Status != 0 {
		table = table.Where("status = ? ", e.Status)
	}

	if e.OpenUserName != "" {
		table = table.Where("open_user_name like ? ", "%"+e.OpenUserName+"%")
	}

	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新OrganMsg
func (e *OrganMsg) Update(id int) (update OrganMsg, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除OrganMsg
func (e *OrganMsg) Delete(id int) (update OrganMsg, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).First(&update).Error; err != nil {
		return
	}

	e.Status = 0
	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

//批量删除
func (e *OrganMsg) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id in (?)", id).Update("status", 0).Error; err != nil {
		return
	}
	Result = true
	return
}
